<?php
/**
 * 活动管理
 *
 *
 *
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class auto_get_mobile_locationControl extends mobileHomeControl{

	public function __construct() {
        parent::__construct();
    }
    
    public function get_localtionOp(){
        $skip=($_REQUEST['skip']-1)*500;//1
        $member_model=Model('member');
        $length=500;
        
        $limit=" $skip,$length ";
        
        $sql='select * from lxs_goods order by goods_id desc limit '.$limit;
        //return $this->table('goods')->limit($limit)->select();

        $member_list= Model()->query($sql);
        //$member_list=Model()->table('member')->select($option);
        $count=0;
        foreach ($member_list as $key=>$val){
            $url='http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel='. $v['member_mobile'].'&t='.time();;
            //$content = $this->get_mobileArea($val['member_mobile']);
            $content=$this->get_mobile_area($val['member_mobile']);
            //__GetZoneResult_ = { mts:'1773660', province:'广西', catName:'中国电信', telString:'17736600405', areaVid:'30518', ispVid:'138238560', carrier:'广西电信' } 	             //$aa=$content->__GetZoneResult_;
            // 	            //echo $content;
            
            preg_match_all("/(\w+):'([^']+)/", $content, $m);
            $mobile = array_combine($m[1], $m[2]);
            $mobile_location=$mobile['carrier'];
            $condition['member_id']=$val['member_id'];
            $data['mobile_attribution']=$mobile_location;
            $result=Model()->table('member')->update($data,$condition);
            $result=$member_model->editMember($condition, $data);
             if($result){
                $temp= '目前写入'.++$count.'条数据,最新写入数据：'.$val['member_mobile'].':'.$mobile_location.'\n';
                echo $temp;
                $myfile = fopen("newfile.txt", "a+") or die("Unable to open file!");
                fwrite($myfile, $temp);
                fclose($myfile);
                
            } 
            //sleep(2);
            
        }
       
        
        //$member_list[$k]['from_local']=$mobile['carrier'];
    }
    
    function get_mobileArea($mobile){
        $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$mobile."&t=".time();
        $content = file_get_contents($url);
        return mb_convert_encoding($content, 'utf-8', 'GBK,UTF-8,ASCII');
    }
    
    /**
     * 抓取网页
     * @param unknown $url
     */
    public function get_mobile_area($mobile){
        $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$mobile;
        //初始化
        $ch = curl_init();
        //设置选项，包括URL
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //函数中加入下面这条语句
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        $output = mb_convert_encoding($output, 'utf-8', 'GBK,UTF-8,ASCII');
       
        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        return ($output);
    }
}